from flask import Blueprint, request
from conn_mysql import ConnectMysql
from .access_token_notify import access_token_verify_func


app_callCar = Blueprint('callCar', 'api')

@app_callCar.route('/callCar', methods=['POST'])
def callCar_api():

    go_time = request.form.get("go_time")#出发时间
    place_departure = request.form.get('place_departure')#出发地
    destination = request.form.get('destination')#目的地
    number_people = request.form.get('number_people')#人数
    phone = request.form.get('phone')#联系电话
    user_id = request.form.get('user_id')#用户id
    carpooling_HitL = request.form.get('carpooling_HitL')#包车还是拼车
    direction = request.form.get('direction')#方向

    access_token = request.form.get('access_token')

    params_list = [go_time, place_departure, destination, 
                   number_people, phone, user_id, carpooling_HitL, direction]

    if not all(params_list):

        return {
            'status' : -1,
            'errorCode': 21,
            'errorMsg': "检查参数，有参数为空",
            'resultBody': None,
        }

    verify_token = access_token_verify_func(access_token, user_id)#验证access_token

    if verify_token['status'] == -1:

        return verify_token
        
    with ConnectMysql() as c:

        sql = '''
        INSERT INTO 
        call_car
        (go_time, place_departure, destination, 
        number_people, phone, user_id, carpooling_HitL, direction)
        VALUES
        (%s,%s,%s,%s,%s,%s,%s,%s);
        '''

        c.insert_info(sql, params_list)

    return {
            'status' : 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "提交成功",
        }



